package com.ellricky.baselib.ui

import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.R
import com.ellricky.baselib.adapter.course.JobTrainingAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.IdNamePickerEntity
import com.ellricky.baselib.entity.new.JobTraining
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEListCallback
import kotlinx.android.synthetic.main.base_fragment_active_doc_child.*

class ActiveDocChildFragment : BaseFragment<Void, BasePresenter<Void>>(), RvRefreshListener,
    AdapterView.OnItemClickListener {

    private var ePickerUtil: EPickerUtil? = null
    private var paramMap: HashMap<String, String> = hashMapOf()
    private var dataSource: List<JobTraining> = ArrayList()
    private var searchDate: String = ""
    private var address: String = ""
    private var target: String = ""
    private var refresh = false

    private val adapter by lazy {
        JobTrainingAdapter(activity!!)
    }

    private val placeList: MutableList<IdNamePickerEntity> = mutableListOf(
        IdNamePickerEntity("自己场内", "自己场内"),
        IdNamePickerEntity("户外", "户外"),
        IdNamePickerEntity("第三方合作", "第三方合作"),
        IdNamePickerEntity("商场内", "商场内")
    )

    private val targetList: MutableList<IdNamePickerEntity> = mutableListOf(
        IdNamePickerEntity("招生", "招生"),
        IdNamePickerEntity("续费", "续费"),
        IdNamePickerEntity("宣传", "宣传")
    )

    override fun getContentLayout(): Int {
        return R.layout.base_fragment_active_doc_child
    }

    override fun initUIView(p0: View?) {
        ePickerUtil = EPickerUtil(mContext)

        tv_active_time.text = searchDate

        rvView.init(this)
        loadDataOnInit()

        rl_time.setOnClickListener {
            ePickerUtil?.showMonthPicker("") { content,
                                               _,
                                               _ ->
                tv_active_time.text = content
                searchDate = content
                loadDataOnInit()
            }
        }

        rl_place.setOnClickListener {
            ePickerUtil?.showIntentPicker(placeList, null) { _,
                                                             t ->
                address = t.name
                loadDataOnInit()
            }
        }

        rl_target.setOnClickListener {
            ePickerUtil?.showClueStatePicker(targetList, null) { _,
                                                                 t ->
                target = t.name
                loadDataOnInit()
            }
        }
    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    private fun initParamMap() {
        paramMap["action"] = "activityDataList"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["search_data"] = searchDate
        paramMap["address"] = address
        paramMap["target"] = target
        paramMap["school_id"] = LocalCacheUtil.getSchoolId()
        paramMap["page"] = rvView.curPage.toString()
        paramMap["page_size"] = "40"
    }

    companion object {
        fun newInstance(): ActiveDocChildFragment {
            return ActiveDocChildFragment()
        }
    }

    override fun setAdapter(rv: RecyclerView) {
        val adapter = adapter
        adapter.setOnItemClickListener(this)
        rv.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        this.refresh = refresh
        val url = EAppUtil.getApiHost()
        initParamMap()
        val callback: NewEBaseCallback = getResponseCallback2()
        HttpUtils.getInstance().postFrom(url, paramMap, callback)
    }

    private fun getResponseCallback2(): NewEBaseCallback {
        return object : NewEListCallback<JobTraining>(mContext) {
            override fun onSuccess(data: MutableList<JobTraining>, msg: String?) {
                dataSource = data
                rvView.onLoadFinish(data, refresh)
            }
        }
    }

    override fun updateAdapterData(p0: Boolean) {
        if (refresh) adapter.refresh(dataSource)
        else adapter.loadMore(dataSource)
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

    }
}